package com.example.dqhl.recycleviewdemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dqhl.recycleviewdemo.R;
import com.example.dqhl.recycleviewdemo.adapter.SimpleImageAdapter;
import com.example.dqhl.recycleviewdemo.modle.ImageData;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.dqhl.recycleviewdemo.widget.CursorWheelLayout;


/**
 * A login screen that offers login via email/password.
 */
public class CoursorWheelActivity extends AppCompatActivity {

    private CursorWheelLayout test_circle_menu_top;
    private TextView tv_getVoice;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coursor_wheel);
        (test_circle_menu_top) = (CursorWheelLayout) findViewById(R.id.test_circle_menu_top);
        (tv_getVoice) = (TextView) findViewById(R.id.tv_getVoice);

        List<ImageData> imageDatas = new ArrayList<ImageData>();
        imageDatas.add(new ImageData(R.mipmap.loading, "0"));
        imageDatas.add(new ImageData(R.mipmap.loading, "1"));
        imageDatas.add(new ImageData(R.mipmap.loading, "2"));
        imageDatas.add(new ImageData(R.mipmap.loading, "3"));
        imageDatas.add(new ImageData(R.mipmap.loading, "4"));
        imageDatas.add(new ImageData(R.mipmap.loading, "5"));
        SimpleImageAdapter simpleImageAdapter = new SimpleImageAdapter(this, imageDatas);
        test_circle_menu_top.setAdapter(simpleImageAdapter);
        test_circle_menu_top.setOnMenuSelectedListener(new CursorWheelLayout.OnMenuSelectedListener() {
            @Override
            public void onItemSelected(CursorWheelLayout parent, View view, int pos) {
                Toast.makeText(CoursorWheelActivity.this, "Top Menu selected position:" + pos, Toast.LENGTH_SHORT).show();
            }
        });
        test_circle_menu_top.setOnMenuItemClickListener(new CursorWheelLayout.OnMenuItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                Toast.makeText(CoursorWheelActivity.this, "Top Menu click position:" + pos, Toast.LENGTH_SHORT).show();

            }
        });

        tv_getVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String[] projection = {MediaStore.Audio.Media._ID,
//                        MediaStore.Audio.Media.DISPLAY_NAME,
//                        MediaStore.Audio.Media.DATA,
//                        MediaStore.Audio.Media.SIZE};
//                String orderBy = MediaStore.Audio.Media.DISPLAY_NAME;
//                Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//                getContentProvider(uri, projection, orderBy);

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");   //打开文件类型
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 1);
            }
        });
    }

    /**
     * 获取ContentProvider
     *
     * @param projection
     * @param orderBy
     */
    public void getContentProvider(Uri uri, String[] projection, String orderBy) {
        List<HashMap<String, String>> listImage = new ArrayList<HashMap<String, String>>();
        Cursor cursor = getContentResolver().query(uri, projection, null, null, orderBy);
        if (null == cursor) {
            return;
        }
        while (cursor.moveToNext()) {
            HashMap<String, String> map = new HashMap<String, String>();
            for (int i = 0; i < projection.length; i++) {
                map.put(projection[i], cursor.getString(i));
                System.out.println(projection[i] + ":::::::" + cursor.getString(i) + "\n");
            }
            listImage.add(map);
        }
        tv_getVoice.setText(listImage.get(0).toString());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            if (requestCode == 1) {
                Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
                String[] proj = {MediaStore.Images.Media.DATA};
                Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
                Cursor cursor1 = getContentResolver().query(uri, proj, null, null, null);
                int actual_image_column_index = cursor1.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                actualimagecursor.moveToFirst();
                String img_path = actualimagecursor.getString(actual_image_column_index);
                File file = new File(img_path);
                Uri photoURI = FileProvider.getUriForFile(CoursorWheelActivity.this, "cn.guanluocang.fileprovider", file);
                Toast.makeText(CoursorWheelActivity.this, photoURI.getPath().toString(), Toast.LENGTH_SHORT).show();
                playMusic(img_path);
            }
        }
    }

    public void playMusic(String filePath) {
//        MediaPlayer player = MediaPlayer.create(this, R.raw.honor);
        MediaPlayer player = new MediaPlayer();
        try {
//            String path = "/storage/emulated/0/honyhor.mp3";
            player.setDataSource(filePath);
            player.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        player.start();
    }

}

